var login_prefix;

/**
 * setupLoginPage
 * Sets up the login page's javascript so it can access the components' elements.
 * @param prefix String prefix for the component-name on the page
 */
function setupLoginPage( prefix )
{
    login_prefix = prefix;
    
    window.onload = setupLoginDocument;
}

/** 
 * setupLoginDocument
 * Sets up events for the login page.
 */
function setupLoginDocument() {
    document.getElementById(login_prefix + '_txtWachtwoord').onselect = cleanPasswordField;
    cleanPasswordField();
}

/**
 * cleanPasswordField
 * Cleans the password field and resets the isHashed field. We want it to be set
 * again when the user fills in a new password.
 */
function cleanPasswordField() {
    document.getElementById(login_prefix + '_hfIsHashed').value = 0;
    document.getElementById(login_prefix + '_txtWachtwoord').value = "";
}

/**
 * validatePassword
 * Validates the password field and hashes it so we won't send un-encrypted passwords
 * over unsecure HTTP.
 * @param source
 * @param arguments
 */
function validatePassword(source, arguments) {
    // Retreive isHashed value.
    var isHashed = document.getElementById(login_prefix + '_hfIsHashed').value;
    
    if(0 == isHashed) {
        // Retreive email and password values.
        var email = document.getElementById(login_prefix + '_txtEmail').value;
        var password = arguments.Value;
        
        // Check password length... if the password field does not contain anything
        // we want a user-error that (s)he forgot to fill in a password.
        if(password.length == 0) {
            arguments.IsValid = false;
            return false;
        }
        
        // Hash the password and set the 'isHashed' element to 1 (true)
        document.getElementById(login_prefix + '_hfIsHashed').value = 1;
        document.getElementById(login_prefix + '_txtWachtwoord').value = hex_md5(email + password);
        
        arguments.IsValid = true;
    }
    return true;
}
